import type { VbenFormSchema } from '@vben/common-ui';
import { formatDateTime } from '@vben/utils';
import { ElTag } from 'element-plus';
import { h } from 'vue';
import { $t } from '#/locales';
import type { LogApi } from '#/api/core/log';

// 异常级别映射 - 与后端枚举保持一致
export const severityMap = {
  // 后端枚举: Warning = 0
  0: { label: $t('system.exceptionLog.severity_level.warning'), type: 'warning' as const },
  // 后端枚举: Error = 1
  1: { label: $t('system.exceptionLog.severity_level.error'), type: 'danger' as const },
  // 后端枚举: Critical = 2
  2: { label: $t('system.exceptionLog.severity_level.critical'), type: 'danger' as const, effect: 'dark' as const },
  // 如果要扩展，可以添加以下值
  // 3: { label: 'Debug', type: 'info' as const, effect: 'plain' as const },
  // 4: { label: 'Information', type: 'info' as const }
};

// 表格列配置
export const columns = [
  {
    type: 'seq' as const,
    title: '序号',
    width: 60,
  },
  {
    field: 'exceptionType',
    title: $t('system.exceptionLog.type'),
    width: 200,
    showOverflow: true,
  },
  {
    field: 'exceptionMessage',
    title: $t('system.exceptionLog.message'),
    minWidth: 300,
    showOverflow: true,
  },
  {
    field: 'requestPath',
    title: $t('system.exceptionLog.path'),
    width: 200,
    showOverflow: true,
  },
  {
    field: 'ipAddress',
    title: $t('system.exceptionLog.ip'),
    width: 130,
  },
  {
    field: 'severity',
    title: $t('system.exceptionLog.severity'),
    width: 100,
    cellRender: {
      name: 'CellTag',
      props: {
        mappingType: 'object',
        mappingData: severityMap,
        defaultTag: { label: '未知', type: 'info', effect: 'light' }
      }
    }
  },
  {
    field: 'exceptionTime',
    title: $t('system.exceptionLog.time'),
    width: 180,
    formatter: ({ cellValue }: { cellValue: string }) => {
      return formatDateTime(cellValue);
    }
  },
];

// 查询表单配置
export const searchFormSchema: VbenFormSchema[] = [
  {
    fieldName: 'exceptionType',
    label: $t('system.exceptionLog.type'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入异常类型',
      clearable: true,
    },
  },
  {
    fieldName: 'severity',
    label: $t('system.exceptionLog.severity'),
    component: 'Select',
    componentProps: {
      options: Object.entries(severityMap).map(([value, item]) => ({
        value: Number(value),
        label: item.label,
      })),
      clearable: true,
    },
  },
  {
    fieldName: 'timeRange',
    label: $t('system.exceptionLog.time'),
    component: 'DatePicker',
    componentProps: {
      type: 'datetimerange',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      rangeSeparator: '至',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      clearable: true,
    },
  },
  {
    fieldName: 'ipAddress',
    label: $t('system.exceptionLog.ip'),
    component: 'Input',
    componentProps: {
      placeholder: '请输入IP地址',
      clearable: true,
    },
  },
  {
    fieldName: 'keyword',
    label: '关键字',
    component: 'Input',
    componentProps: {
      placeholder: '异常信息/请求路径',
      clearable: true,
    },
  },
];
